package com.xpertline.ui.main;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import org.jdesktop.swingx.JXBusyLabel;
import org.jdesktop.swingx.JXLoginPane;
import org.jdesktop.swingx.JXLoginPane.JXLoginDialog;
import org.jdesktop.swingx.plaf.windows.WindowsLookAndFeelAddons;

import com.xpertline.ui.booking.ListBookingPane;
import com.xpertline.ui.config.ConfigPane;
import com.xpertline.ui.customer.ListCustomerPane;
import com.xpertline.ui.floor.ListFloorPane;
import com.xpertline.ui.food.ListFoodPane;
import com.xpertline.ui.invoice.RoomInfoPane;
import com.xpertline.ui.payment.ListPaymentPane;
import com.xpertline.ui.payment.StatisticPaymentPane;
import com.xpertline.ui.room.ListRoomPane;
import com.xpertline.ui.specialday.ListSpecialDayPane;
import com.xpertline.ui.staff.ListStaffPane;
import com.xpertline.ui.supplier.ListSupplierPane;
import com.xpertline.ui.unit.ListUnitPane;
import com.xpertline.ui.user.ListUserPane;
import com.xpertline.ui.store.ListStorePane;
import de.javasoft.plaf.synthetica.SyntheticaWhiteVisionLookAndFeel;
import de.javasoft.swing.AboutDialog;

public class MainFrame {

	private JFrame frame;
	private JTree tree;
	private JPanel leftPanel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager
							.setLookAndFeel(new SyntheticaWhiteVisionLookAndFeel());
					MainFrame window = new MainFrame();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainFrame() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 812, 489);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setExtendedState(Frame.MAXIMIZED_BOTH);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
		frame.getContentPane().setLayout(gridBagLayout);

		final JSplitPane splitPane = new JSplitPane();
		GridBagConstraints gbc_splitPane = new GridBagConstraints();
		gbc_splitPane.fill = GridBagConstraints.BOTH;
		gbc_splitPane.gridx = 0;
		gbc_splitPane.gridy = 0;
		frame.getContentPane().add(splitPane, gbc_splitPane);

		leftPanel = new JPanel();
		leftPanel.setMinimumSize(new Dimension(250, 10));
		splitPane.setLeftComponent(leftPanel);
		GridBagLayout gbl_leftPanel = new GridBagLayout();
		gbl_leftPanel.columnWidths = new int[] { 0, 0 };
		gbl_leftPanel.rowHeights = new int[] { 0, 0 };
		gbl_leftPanel.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gbl_leftPanel.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
		leftPanel.setLayout(gbl_leftPanel);

		tree = new JTree();
		tree.addTreeSelectionListener(new TreeSelectionListener() {
			public void valueChanged(TreeSelectionEvent arg0) {
				final String basicSettingPath = "Thiet lap co ban";
				final String userRegisterPath = "Phan quyen su dung";
				final String floorRegisterPath = "Dang ki tang lau";
				final String specialDayRegisterPath = "Dang ki ngay dac biet";
				final String roomRegisterPath = "Dang ki phong";
				final String customerRegisterPath = "Dang ki khach hang";
				final String unitRegisterPath = "Dang ki don vi";
				final String supplierRegisterPath = "Dang ki nha cung cap";
				final String staffRegisterPath = "Dang ki nhan vien";
				final String foodRegisterPath = "Dang ki thuc an";
				final String bookingRegisterPath = "Thong tin dat phong";
				final String invoiceRegisterPath = "Thanh toan tien phong";
				final String retailRegisterPath = "Hoa don ban le";
				final String controlPaymentPath = "Kiem soat thu chi";
				final String moneyPaymentPath = "Luong tien thu chi";
				final String inputStorePath = "Thong tin nhap kho";
				Component rightPane = null;
				final String lastPathCompName = arg0.getPath()
						.getLastPathComponent().toString();
				if (lastPathCompName.equals(basicSettingPath)) {
					rightPane = new ConfigPane();
				} else if (lastPathCompName.equals(userRegisterPath)) {
					rightPane = new ListUserPane();
				} else if (lastPathCompName.equals(floorRegisterPath)) {
					rightPane = new ListFloorPane();
				} else if (lastPathCompName.equals(specialDayRegisterPath)) {
					rightPane = new ListSpecialDayPane();
				} else if (lastPathCompName.equals(roomRegisterPath)) {
					rightPane = new ListRoomPane();

				} else if (lastPathCompName.equals(customerRegisterPath)) {
					rightPane = new ListCustomerPane();

				} else if (lastPathCompName.equals(unitRegisterPath)) {
					rightPane = new ListUnitPane();

				} else if (lastPathCompName.equals(supplierRegisterPath)) {
					rightPane = new ListSupplierPane();
				} else if (lastPathCompName.equals(staffRegisterPath)) {
					rightPane = new ListStaffPane();

				} else if (lastPathCompName.equals(foodRegisterPath)) {
					rightPane = new ListFoodPane();

				} else if (lastPathCompName.equals(bookingRegisterPath)) {
					rightPane = new ListBookingPane();

				} else if (lastPathCompName.equals(invoiceRegisterPath)) {
					rightPane = new RoomInfoPane();

				} else if (lastPathCompName.equals(retailRegisterPath)) {
					rightPane = new ListPaymentPane();

				} else if (lastPathCompName.equals(controlPaymentPath)) {
					rightPane = new StatisticPaymentPane();

				} else if (lastPathCompName.equals(moneyPaymentPath)) {
					rightPane = new ListPaymentPane();

				} else if (lastPathCompName.equals(inputStorePath)) {
					rightPane = new ListStorePane();
				}
				JXBusyLabel busyLabel = new JXBusyLabel();
				busyLabel.setBusy(true);
				splitPane.setRightComponent(rightPane);
				busyLabel.setBusy(false);
			}
		});
		tree.setRootVisible(false);
		tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode(
				"C\u1EA5u h\u00ECnh") {
			{
				add(new DefaultMutableTreeNode("Thiet lap co ban"));
				add(new DefaultMutableTreeNode("Phan quyen su dung"));
				add(new DefaultMutableTreeNode("Dang ki tang lau"));
				add(new DefaultMutableTreeNode("Dang ki ngay dac biet"));
				add(new DefaultMutableTreeNode("Dang ki phong"));
				add(new DefaultMutableTreeNode("Dang ki khach hang"));
				add(new DefaultMutableTreeNode("Dang ki don vi"));
				add(new DefaultMutableTreeNode("Dang ki nha cung cap"));
				add(new DefaultMutableTreeNode("Dang ki nhan vien"));
				add(new DefaultMutableTreeNode("Dang ki thuc an"));
				add(new DefaultMutableTreeNode("Thong tin dat phong"));
				add(new DefaultMutableTreeNode("Thanh toan tien phong"));
				add(new DefaultMutableTreeNode("Hoa don ban le"));
				add(new DefaultMutableTreeNode("Kiem soat thu chi"));
				add(new DefaultMutableTreeNode("Luong tien thu chi"));
				add(new DefaultMutableTreeNode("Thong tin nhap kho"));
			}
		}));
		GridBagConstraints gbc_tree = new GridBagConstraints();
		gbc_tree.fill = GridBagConstraints.BOTH;
		gbc_tree.gridx = 0;
		gbc_tree.gridy = 0;
		leftPanel.add(tree, gbc_tree);
		splitPane.setDividerLocation(250);

		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);

		JMenu mnNewMenu = new JMenu("Menu");
		menuBar.add(mnNewMenu);

		JMenuItem loginItem = new JMenuItem("Login");
		loginItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JXLoginPane loginPane = new JXLoginPane();
				JXLoginDialog loginDialog = new JXLoginDialog(frame, loginPane);
				loginDialog.setModal(true);
				loginDialog.setVisible(true);
			}
		});
		mnNewMenu.add(loginItem);

		JMenuItem userItem = new JMenuItem("User");
		// userItem.addActionListener(new ActionListener() {
		// public void actionPerformed(ActionEvent arg0) {
		// rightPanel.add(new UserListPane());
		// rightPanel.revalidate();
		// }
		// });
		mnNewMenu.add(userItem);

		JMenuItem exitItem = new JMenuItem("Exit");
		mnNewMenu.add(exitItem);

		JMenu helpMenu = new JMenu("Help");
		menuBar.add(helpMenu);

		JMenuItem helpItem = new JMenuItem("Help Content");
		helpMenu.add(helpItem);

		JMenuItem aboutItem = new JMenuItem("About XpertLine");
		aboutItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				try {
					AboutDialog.showDialog(SwingUtilities
							.getWindowAncestor((Component) evt.getSource()),
							null, "About this application.", "My About Text");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		helpMenu.add(aboutItem);
	}

}
